package com.uphone.kingmall.main;

import android.text.TextUtils;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;

import com.china.zhuoyue.R;
import com.lzy.okgo.model.Response;
import com.uphone.kingmall.base.BaseActivity;
import com.uphone.kingmall.listener.onNormalRequestListener;
import com.uphone.kingmall.login.LoginActivity;
import com.uphone.kingmall.utils.CommonUtil;
import com.uphone.kingmall.utils.FragmentMgr;
import com.uphone.kingmall.utils.GsonUtils;
import com.uphone.kingmall.utils.MyUrl;
import com.uphone.kingmall.utils.OkGoUtils;
import com.uphone.kingmall.utils.SharedPreferencesHelper;
import com.uphone.kingmall.utils.version.VersionBean;
import com.uphone.kingmall.utils.version.VersionUtil;

import java.util.List;

import butterknife.BindView;

/**
 * author : SuMeng
 * e-mail : 986335838@qq.com
 * time   : 2019/02/15
 * desc   : APP主页面
 */
public class MainActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {

    @BindView(R.id.first_tab_container)
    FrameLayout firstTabContainer;
    @BindView(R.id.main_home_home_radioButton)
    RadioButton mainHomeHomeRadioButton;
    @BindView(R.id.main_home_class_radioButton)
    RadioButton mainHomeClassRadioButton;
    @BindView(R.id.main_home_circle_radioButton)
    RadioButton mainHomeCircleRadioButton;
    @BindView(R.id.main_home_shopcar_radioButton)
    RadioButton mainHomeShopcarRadioButton;
    @BindView(R.id.main_home_my_radioButton)
    RadioButton mainHomeMyRadioButton;
    @BindView(R.id.main_home_bottom_layout)
    RadioGroup mainHomeBottomLayout;

    private FragmentMgr mFragmentMgr;

    @Override
    public int getLayoutContentId() {
        isUseImmersionBar = true;
        return R.layout.kingmall_activity_main;
    }

    @Override
    public void loadJPush() {
        super.loadJPush();
        FragmentManager manager = mFragmentMgr.getManager();
        if (manager != null) {
            List<Fragment> fragments = manager.getFragments();
            for (int i = 0; i < fragments.size(); i++) {
                if (fragments.get(i) != null) {
                    if (fragments.get(i) instanceof HomeFragment) {
                        ((HomeFragment) fragments.get(i)).loadJPush();
                    } else if (fragments.get(i) instanceof MyFragment) {
                        ((MyFragment) fragments.get(i)).loadJPush();
                    }
                }
            }
        }
    }

    public void reloadShoppingCart() {
        if (mFragmentMgr.getCurrFragment() != null && mFragmentMgr.getCurrFragment() instanceof ShopCarFragment) {
            ((ShopCarFragment) mFragmentMgr.getCurrFragment()).loadData();
        }
    }

    @Override
    public void initView() {
        mainHomeBottomLayout.setOnCheckedChangeListener(this);
    }

    @Override
    public void initData() {
        mFragmentMgr = new FragmentMgr(this);
        mFragmentMgr.setContainerId(R.id.first_tab_container);
        mFragmentMgr.showTabFragment(HomeFragment.class);
//        CommonUtil.loginRY();
//        isNewVersion();
    }

    @Override
    public void onCheckedChanged(RadioGroup buttonView, int checkedId) {
        switch (checkedId) {
            case R.id.main_home_home_radioButton:
                mImmersionBar
//                .statusBarView(statusBar)
                        .fitsSystemWindows(false)
                        .statusBarColor(R.color.colorTransparent)
                        .statusBarDarkFont(true, 0.2f)
                        .navigationBarColor(R.color.black)
                        .fullScreen(false)
                        .init();
                mFragmentMgr.showTabFragment(HomeFragment.class);
                break;
            case R.id.main_home_class_radioButton:
                mImmersionBar
//                .statusBarView(statusBar)
                        .fitsSystemWindows(true)
                        .statusBarColor(R.color.colorWhite)
                        .statusBarDarkFont(true, 0.2f)
                        .navigationBarColor(R.color.black)
                        .fullScreen(false)
                        .init();
                mFragmentMgr.showTabFragment(ClassFragment.class);

                break;
            case R.id.main_home_circle_radioButton:
                mImmersionBar
//                .statusBarView(statusBar)
                        .fitsSystemWindows(true)
                        .statusBarColor(R.color.colorWhite)
                        .statusBarDarkFont(true, 0.2f)
                        .navigationBarColor(R.color.black)
                        .fullScreen(false)
                        .init();
                mFragmentMgr.showTabFragment(CricleFragment.class);
                break;
            case R.id.main_home_shopcar_radioButton:
                if (TextUtils.isEmpty(SharedPreferencesHelper.getToken())) {
                    CommonUtil.startIntent(this, LoginActivity.class);
                    mainHomeHomeRadioButton.setChecked(true);
                    return;
                }
                mImmersionBar
//                .statusBarView(statusBar)
                        .fitsSystemWindows(true)
                        .statusBarColor(R.color.colorWhite)
                        .statusBarDarkFont(true, 0.2f)
                        .navigationBarColor(R.color.black)
                        .fullScreen(false)
                        .init();
                mFragmentMgr.showTabFragment(ShopCarFragment.class);
                break;
            case R.id.main_home_my_radioButton:
                if (TextUtils.isEmpty(SharedPreferencesHelper.getToken())) {
                    CommonUtil.startIntent(this, LoginActivity.class);
                    mainHomeHomeRadioButton.setChecked(true);
                    return;
                }
                mImmersionBar
//                .statusBarView(statusBar)
                        .fitsSystemWindows(false)
                        .statusBarColor(R.color.colorTransparent)
                        .statusBarDarkFont(true, 0.2f)
                        .navigationBarColor(R.color.black)
                        .fullScreen(false)
                        .init();
                mFragmentMgr.showTabFragment(MyFragment.class);
                break;
            default:
                break;
        }
    }


    /**
     * 是否有最新版本
     */
    private void isNewVersion() {
        OkGoUtils.normalRequestGet(MyUrl.updateApk, new onNormalRequestListener() {
            @Override
            public void onSuccess(String body, int code) {
                VersionBean bean = GsonUtils.getGson().fromJson(body, VersionBean.class);
                if (bean != null && bean.code == 0 && !VersionUtil.getAppVersion().equals(bean.data.version)) {
                    VersionUtil.showAlertDialog(MainActivity.this, bean.data);
                }
            }

            @Override
            public void onError(Response<String> response) {
            }
        });
    }

}
